//
//  BreakPointViewController.m
//  StudyDemo
//
//  Created by 赵忠鹏 on 2018/5/4.
//  Copyright © 2018年 zhaozhongpeng. All rights reserved.
//

#import "BreakPointViewController.h"

@interface BreakPointViewController ()
@property (weak, nonatomic) IBOutlet UIProgressView *breakPointProgress;

@property (nonatomic,assign) int totalLength;

@property (nonatomic,copy) NSString *fileName;

@property (nonatomic,copy) NSString *filePath;

@property (nonatomic,assign) int currentLength;

@property (nonatomic,strong) NSURLConnection *connect;
// 输出流
@property (nonatomic,strong) NSOutputStream *stream;

@end

@implementation BreakPointViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
}
// 开始
- (IBAction)beginbuttonClick:(id)sender {
    //1.确定请求路径
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_04.mp4"];
    //2.创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    /*
     表示头500个字节：Range: bytes=0-499
     表示第二个500字节：Range: bytes=500-999
     表示最后500个字节：Range: bytes=-500
     表示500字节以后的范围：Range: bytes=500-
     */
    NSString *range = [NSString stringWithFormat:@"bytes=%d-",self.currentLength];
    [request setValue:range forHTTPHeaderField:@"Range"];
    NSLog(@"%@",range);
    
    //3.设置代理,发送请求
    self.connect =  [NSURLConnection connectionWithRequest:request delegate:self];
   
}
// 暂停
- (IBAction)stopButtonClick:(id)sender {
    NSLog(@"暂停--------------------%d-",self.currentLength);
    [self.connect cancel];
}

/*
 1.接收到服务器响应的适合调用
 */
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"+++++++++++");
    
    if (self.currentLength > 0) {
        return;
    }
    NSFileManager *manager = [NSFileManager defaultManager];
    
    //保存下载的文件到沙河
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    //拼接文件全路径
    self.filePath = [caches stringByAppendingPathComponent:response.suggestedFilename];
    
    NSLog(@"self.filePath - %@",self.filePath);
    
    //创建文件
    [manager createFileAtPath:self.filePath contents:nil attributes:nil];
    
    
    //拿到文件的总大小
    self.totalLength = response.expectedContentLength + self.currentLength;
    
    NSLog(@"self.totalLength - %d",self.totalLength);
    
    //创建输出流
    //追加
    self.stream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES];
    
    //开启
    [self.stream open];
    
}

/*
 2.接收到服务器返回的数据,会调用多次
 */
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    
    self.currentLength +=data.length;
    
    //写数据
    /*
     第一个参数:要写入的二进制数据
     第二个参数:数据的大小
     */
    [self.stream write:data.bytes maxLength:data.length];
    
    self.breakPointProgress.progress = 1.0 *self.currentLength / self.totalLength;
}

/*
 3.当请求完成之后调用该方法
 */
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading");
    
    [self.stream close];
    self.stream = nil;
}
/*
 4.当请求失败的适合调用该方法,如果失败那么error有值
 */
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError");
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
